﻿using UnityEngine;
using UnityEditor;
using System.Collections.Generic;

namespace CWM.Skinn
{
    public static partial class SkinnContextCommands
    {
        [CtxMenuItem("SkinnedMeshRenderer/Shapes/Sync",
           "Set all blend-shape float values under the same root to match a renderer.", ContextRequirements.SingleItem, 0)]
        public static void SkinnedMeshRendererShapesSyncValues()
        {
            foreach (var item in SkinnContext.GetContextItems())
            {
                var context = item.GetComponent<SkinnedMeshRenderer>();
                if (SkinnEx.IsNullOrNotInAScene(context)) continue;
                context.SyncShapesToThis(null, "Sync Shapes");
                SkinnEditorEx.RepaintSceneView();
            }

            SkinnContext.Finished();
        }

        [CtxMenuItem("SkinnedMeshRenderer/Shapes/Log",
         "Logs the index and name of each blend-shape in a mesh.", ContextRequirements.HasBlendshapes, 001)]
        public static void SkinnedMeshRendererShapesLog()
        {
            foreach (var item in SkinnContext.GetContextItems())
            {
                var context = item.GetComponent<SkinnedMeshRenderer>();
                if (SkinnEx.IsNullOrNotInAScene(context)) continue;
                if (SkinnEx.IsNullOrNotVailid(context)) continue;

                var sharedMesh = context.sharedMesh;
                var info = SkinnEx.EnforceObjectName(context.sharedMesh) + System.Environment.NewLine;
                var blendShapeCount = sharedMesh.blendShapeCount;
                for (int i = 0; i < blendShapeCount; i++)
                {
                    info += string.Format(" {0}: {1}", i, sharedMesh.GetBlendShapeName(i));
                    info += System.Environment.NewLine;
                }

                Debug.Log(info);
            }

            SkinnContext.Finished();
        }
    }
}